﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Mover : MonoBehaviour {
    #region fields
    [Tooltip("当前目标地点")]
    public Vector3 tagertPos;
    [SerializeField, Tooltip("移动速度")]
    public float velocity=2f;
    [SerializeField, Tooltip("采用的移动方式")]
    private MoveStyle moveStyle = MoveStyle.Normal;
    [SerializeField, Tooltip("矫正力。使速度维持在目标速度的力")]
    private float force = 1f;
    private Rigidbody rig;


    public Transform selfTransform;//缓存自身的transform,只读
    #endregion
    #region  method

    private void Awake()
    {
        selfTransform = transform;
        rig = GetComponent<Rigidbody>();
        tagertPos = selfTransform.position;//初始化位置，免得跑偏
        
    }
    #region move method

    /// <summary>
    /// 直接移动position的方式
    /// 移动方法，默认为以速度v移动
    /// </summary>
    /// <param name="v"></param>
    protected virtual void Move(Vector3 v)
    {
      //  print(v);
        selfTransform.position = selfTransform.position + v * Time.deltaTime;
        //selfTransform.Translate(v * Time.deltaTime);
    }



    /// <summary>
    /// 以速度velocity朝目标匀速移动
    /// </summary>
    /// <param name="pos"></param>
    protected void VMove(Vector3 pos)
    {
       //print(pos + "pos");
       // print(selfTransform.position+"self");
        Vector3 dir = pos - selfTransform.position;
        if(dir.magnitude<=velocity*Time.deltaTime*2f)return;//如果距离比一次步长短就不再移动
        //print("move"+ dir.normalized); 
        Move(dir.normalized * velocity);
    }


    /// <summary>
    /// 跟随目标.需要时调用
    /// </summary>
    protected virtual void Pursue()
    {
       // print("tagert" + tagertPos);
        VMove(tagertPos);
    }

 
    #endregion




    private void Update()
    {
       Normal_Move();
    }


    #region Move Style Mehtod

    public virtual void Normal_Move()
    {
      //  print("purse");
        Pursue();
    }

    private void RigidForce_Move()
    {
        if (Time.timeScale == 0) return;

        Vector3 charp = tagertPos - selfTransform.position;
        //print(charp);
        rig.Move(new Vector3(charp.x, charp.y, 0).normalized, force);
    }

    private void RigidPower_Move()
    {
        print("未完成");
    }

    /// <summary>
    /// 是否到达目标位置
    /// </summary>
    /// <returns></returns>
    public bool Arrived()
    {
        if (Mathf.Abs((selfTransform.position-tagertPos).magnitude) <= velocity * Time.deltaTime * 2f)
            return true;
        return false;
    }


    #endregion
    #endregion
}


#region rigdMove

/// <summary>
/// 刚体运动方式
/// </summary>
public static class RigidMove
{
    /// <summary>
    /// 用线性力矫正速度的运动方式.在Updata中调用
    /// </summary>
    /// <param name="rig"></param>
    /// <param name="v"></param>
    /// <param name="force"></param>
    public static void Move(this Rigidbody rig, Vector3 v, float force)
    {
        Vector3 charp = v - rig.velocity;//差值
       // Debug.Log(charp);
        rig.AddForce(charp.normalized * force);
    }

  

}

#endregion
#region MoveStyle
/// <summary>
/// 移动方式
/// 刚体移动，普通变换移动
/// </summary>
public enum MoveStyle
{
    RigidForce, Normal, RigidPower,
}

#endregion